/**
 * jims
 */
package com.jims.master.modules.csm.web;

import com.jims.master.common.data.StringData;
import com.jims.master.common.persistence.Page;
import com.jims.master.common.utils.StringUtils;
import com.jims.master.common.web.BaseController;
import com.jims.master.modules.csm.entity.ExpNameDict;
import com.jims.master.modules.csm.entity.ExpPrice;
import com.jims.master.modules.csm.service.ExpDictService;
import com.jims.master.modules.csm.service.ExpNameDictService;
import com.jims.master.modules.csm.service.ExpPriceService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;

/**
 * 消耗品价格Controller
 * @author CTQ
 * @version 2017-03-02
 */
@Controller
@RequestMapping(value = "${adminPath}/csm/expPrice")
public class ExpPriceController extends BaseController {

	@Autowired
	private ExpPriceService expPriceService;
	@Autowired
	private ExpNameDictService expNameDictService;
	@Autowired
	private ExpDictService expDictService;

	@ModelAttribute
	public ExpPrice get(@RequestParam(required=false) String id) {
		ExpPrice entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = expPriceService.get(id);
		}
		if (entity == null){
			entity = new ExpPrice();
		}
		return entity;
	}

	/**
	 * @param expNameDict 传递参数
	 * @return String    返回类型
	* @throws
	 * @Title: priceIndex
	 * @Desciption: (消耗品价格主页列表)
	 * @author CTQ
	 * @date 2017-03-09
	 */
	@RequiresPermissions("user")
	@RequestMapping(value = {"priceIndex", ""})
	public String priceIndex(ExpNameDict expNameDict, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<ExpNameDict> namepage = expNameDictService.findPageByInfo(new Page<ExpNameDict>(request, response), expNameDict);
		namepage.setFuncName("namepage");
		model.addAttribute("namepage", namepage);
		Page<ExpPrice> page = expPriceService.findPage(new Page<ExpPrice>(request, response), new ExpPrice());
		model.addAttribute("page", page);
		return "modules/csm/expNamePriceIndex";
	}

    /**
     * 消耗品价格维护首页
     * @param expPrice
     * @param request
     * @param response
     * @param model
     * @return
     */
	@RequiresPermissions("user")
	@RequestMapping(value = {"index", ""})
	public String list(ExpPrice expPrice, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<ExpPrice> page = expPriceService.findPage(new Page<ExpPrice>(request, response), expPrice);
		model.addAttribute("expPrice",expPrice);
		model.addAttribute("page", page);
		return "modules/csm/expPriceIndex";
	}

	/**
	 * 查询消耗品价格
	 * @param expPrice
	 * @param request
	 * @param response
	 * @param model
	 * @return
	 */
	@RequiresPermissions("user")
	@RequestMapping(value = {"query", ""})
	public String querylist(ExpPrice expPrice, HttpServletRequest request, HttpServletResponse response, Model model) {
		expPrice.updateOrgId();
		Page<ExpPrice> page = expPriceService.findListPrice(new Page<ExpPrice>(request, response), expPrice);
		model.addAttribute("expPrice",expPrice);
		model.addAttribute("page", page);
		return "modules/csm/expPriceQueryIndex";
	}

	/**
	 * 查询消耗品历史价格
	 * @param expPrice
	 * @param request
	 * @param response
	 * @param model
	 * @return
	 */
	@RequiresPermissions("user")
	@RequestMapping(value = {"query1", ""})
	public String querylistlog(ExpPrice expPrice, HttpServletRequest request, HttpServletResponse response, Model model) {
		expPrice.updateOrgId();
		Page<ExpPrice> page = expPriceService.findListPriceLog(new Page<ExpPrice>(request, response), expPrice);
		model.addAttribute("expPrice",expPrice);
		model.addAttribute("page", page);
		return "modules/csm/expPriceQueIndex";
	}

	@RequiresPermissions("user")
	@RequestMapping(value = "form")
	public String form(ExpPrice expPrice, Model model) {
		expPrice.setStartDate(new Date());
		model.addAttribute("expPrice", expPrice);
		return "modules/csm/expPriceForm";
	}

	@RequiresPermissions("user")
	@RequestMapping(value = "save")
	@ResponseBody
	public StringData save(ExpPrice expPrice, Model model) {
	    StringData data=new StringData();
		int i=expPriceService.savePrice(expPrice);
        if(i>0){
            data.setCode("success");
            data.setData("保存消耗品价格成功");
        }else if(i == -1){
            data.setCode("error");
			data.setData("该厂家规格的价格已经存在,请不要重复维护!");
        }else {
            data.setCode("error");
            data.setData("保存消耗品价格失败");
        }
        return data;
	}
	@RequiresPermissions("user")
	@RequestMapping(value = "delete")
	@ResponseBody
	public StringData delete(ExpPrice expPrice) {
	    StringData data=new StringData();
		int i=expPriceService.delete(expPrice);
        if(i>0){
            data.setCode("success");
            data.setData("删除消耗品价格成功");
        }else{
            data.setCode("error");
            data.setData("删除消耗品价格失败");
        }
		return data;
	}

    /**
     * 生成基本价格
     * @param expPrice
     * @return
     */
	@RequiresPermissions("user")
	@RequestMapping(value = "basicPrice")
	@ResponseBody
	public StringData basicPrice(ExpPrice expPrice) {
		StringData data=new StringData();
		int i=expPriceService.basicPrice(expPrice);
		if(i>0){
			data.setCode("success");
			data.setData("生成消耗品基本价格成功");
		}else if(i==-1){
			data.setCode("info");
			data.setData("该消耗品基本价格已生成，不能重复生成");
		}else{
			data.setCode("error");
			data.setData("该消耗品已是基本价格，不需要再生成");
		}
		return data;
	}

    /**
     * 消耗品停价功能
     * @param expPrice
     * @return
     * @author fengyg
     */
    @RequiresPermissions("user")
    @RequestMapping(value = "stopPrice")
    @ResponseBody
    public StringData stopPrice(ExpPrice expPrice){
        return expPriceService.stopPrice(expPrice);
    }

    /*
    * 价表调价加载主页
    * @zhuq
    * */
    @RequiresPermissions("user")
    @RequestMapping(value = {"modify", ""})
    public String priceModify(ExpNameDict expNameDict, HttpServletRequest request, HttpServletResponse response, Model model) {
        Page<ExpNameDict> namepage = expNameDictService.findPage(new Page<ExpNameDict>(request, response), expNameDict);
        model.addAttribute("namepage", namepage);
        Page<ExpPrice> page = expPriceService.findPage(new Page<ExpPrice>(request, response), new ExpPrice());
        model.addAttribute("page", page);
        return "modules/csm/expNamePriceModify";
    }

    /*
    * 价表调价调整页
    * @zhuq
    * */
    @RequiresPermissions("user")
    @RequestMapping(value = "formModify")
    public String formModify(ExpPrice expPrice, Model model) {
        model.addAttribute("expPrice", expPrice);
        return "modules/csm/expPriceFormModify";
    }

    /*
    * 价表调价加载消耗品价格列表
    * @zhuq
    * */
    @RequiresPermissions("user")
    @RequestMapping(value = {"indexModify", ""})
    public String listModify(ExpPrice expPrice, HttpServletRequest request, HttpServletResponse response, Model model) {
        Page<ExpPrice> page = expPriceService.findByMaxSpec(new Page<ExpPrice>(request, response), expPrice);
        model.addAttribute("expPrice",expPrice);
        model.addAttribute("page", page);
        return "modules/csm/expPriceIndexModify";
    }



    /*
    * 调整消耗品价格
    * @zhuq
    * */
    @RequiresPermissions("user")
    @RequestMapping(value = "saveModify")
    @ResponseBody
    public StringData saveModify(ExpPrice expPrice, Model model) {
        StringData data=new StringData();
        int i=expPriceService.savePriceModify(expPrice);
        if(i>0){
            data.setCode("success");
            data.setData("消耗品价格调整成功");
        }else{
            data.setCode("error");
            data.setData("消耗品价格调整失败");
        }
        return data;
    }

}